# coding:utf-8
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.core.urlresolvers import reverse
from .forms import AddForm

from django.template import RequestContext


'''#下面四句好使
a = request.POST['a']
b = request.POST['b']
c = int(a) + int(b)
return HttpResponse(str(c))
'''
def index(request):
    # 当提交表单时
    if request.method == 'POST':
        # form 包含提交的数据
        form = AddForm(request.POST)
        # 如果提交的数据合法
        if form.is_valid():
            a = form.cleaned_data['a']
            b = form.cleaned_data['b']

            return HttpResponse(str(int(a) + int(b)))

    else:
        form = AddForm()

    return render(request, 'testapp/index.html', {'form': form})

def first(request):
    return render(request, 'testapp/index.html')

def add(request):
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))

def add2(request, a, b):
    c = int(a) + int(b)
    return HttpResponse(str(c)+"你好我是c")

def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(reverse('add2', args=(a, b)))

def jumptohomepage(request):
    return HttpResponseRedirect(reverse('homepage'))

def homepage(request):
    return render(request, 'testapp/home.html')

def print_string(request):
    string = u'这里是一个Django测试'
    return render(request, 'testapp/print_string.html', {'string': string})

def for_loop(request):
    turoriallist = ['html', 'css', 'jquery', 'python', 'django']
    return render(request, 'testapp/for_loop.html', {'turoriallist': turoriallist})

def print_dict(request):
    info_dict = {'site': u'mysite','content': u'显示字典内容'}
    return render(request, 'testapp/print_dict.html', {'info_dict': info_dict})

def shouye(request):
    dict = {'name':u'赵铭富','content':u'前进'}
    return render(request,'testapp/shouye.html',{'dict':dict})

def forloop_if(request):
    list = map(str, range(100))
    return render(request, 'testapp/forloop_if.html', {'list': list})

def use_if(request):
    value = 59
    return render(request, 'testapp/use_if.html', {'value': value})

def get_url_user(request):
    delete = 0
    return render(request, 'testapp/get_url_user.html', {'delete': delete})
